package com.q.tqb.util.oss;

import android.os.Handler;
import android.util.Log;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
import com.alibaba.sdk.android.oss.model.OSSRequest;
import com.alibaba.sdk.android.oss.model.OSSResult;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.q.tqb.base.AppConst;
import com.q.tqb.base.BaseApplication;
import com.q.tqb.util.LogUtil;
import com.q.tqb.util.http.IRawResponse;

import java.io.File;
import java.util.HashMap;

/**
 * Created by oss on 2015/12/7 0007.
 * 支持普通上传，普通下载和断点上传
 */
public class OssService {

    public static void asyncPutFile(String object, String localFile, OssCallback myCallback) {
        if (object.equals("")) {
            LogUtil.i("保存文件名不能为空");
            return;
        }

        File file = new File(localFile);
        if (!file.exists()) {
            LogUtil.i("文件不存在或文件错误");
            return;
        }

        // 构造上传请求
        PutObjectRequest put = new PutObjectRequest(AppConst.OSS_BUCKET, object, localFile);


        // 传入对应的上传回调参数，这里默认使用OSS提供的公共测试回调服务器地址
        put.setCallbackParam(new HashMap<String, String>() {
            {
                put("callbackUrl", AppConst.OSS_ADDRESS);
              //  put("callbackHost", "oss-cn-shenzhen.aliyuncs.com");
                put("callbackBodyType", "application/json");
                put("callbackBody", "{\"mimeType\":${mimeType},\"size\":${size}}");
            }
        });

        OSSAsyncTask task = BaseApplication.sOSS.asyncPutObject(put, myCallback);
    }


}
